package cao.oschina.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import hd.util.ScreenHelper;

/**
 * TabView
 * <p/>
 * Created by Haidy on 15/3/6.
 */
public class TabView extends ViewGroup {

    private OnCheckedChangedListener onCheckedChangedListener;
    private int checkedId = -1;

    public TabView(Context context) {
        super(context);
    }

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getMeasuredWidth();
        final float ITEM_WIDTH = width / (float) getChildCount();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof TabItemView) {
                child.layout((int) (i * ITEM_WIDTH), t, (int) ((i + 1) * ITEM_WIDTH), b);
            } else {
                int offset = (getMeasuredHeight() - ScreenHelper.dp2px(getContext(), 30)) / 2;
                child.layout((int) (i * ITEM_WIDTH) + offset, offset, (int) ((i + 1) * ITEM_WIDTH) - offset, getMeasuredHeight() - offset);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
        width = getMeasuredWidth();
        final float ITEM_WIDTH = width / (float) getChildCount();
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(MeasureSpec.makeMeasureSpec((int) ITEM_WIDTH, MeasureSpec.getMode(widthMeasureSpec)), heightMeasureSpec);
        }
    }

    public void setOnCheckedChangedListener(OnCheckedChangedListener onCheckedChangedListener) {
        this.onCheckedChangedListener = onCheckedChangedListener;
        if (this.onCheckedChangedListener != null) {
            for (int i = 0; i < getChildCount(); i++) {
                getChildAt(i).setOnClickListener(listener);
            }
        }
    }

    private OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v instanceof TabItemView) {
                if (checkedId != v.getId()) {
                    check(v.getId());
                }
            } else {
                if (onCheckedChangedListener != null) {
                    onCheckedChangedListener.onOtherClicked(v);
                }
            }
        }
    };

    /**
     * 选中id为id的item
     *
     * @param id id
     */
    public void check(int id) {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof TabItemView) {
                if (child.getId() == id) {
                    ((TabItemView) child).check(true);
                    if (onCheckedChangedListener != null) {
                        onCheckedChangedListener.onChanged((TabItemView) child);
                    }
                } else {
                    ((TabItemView) child).check(false);
                }
            }
        }
    }

    public interface OnCheckedChangedListener {
        void onChanged(TabItemView tabItemView);

        void onOtherClicked(View view);
    }
}
